package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.EmployeeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.EmployeeCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.EmployeeService;
import com.cskaoyan.wordmemorize.service.FileUploadService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;

@RestController
@RequestMapping("/admin/employee")
public class AdminEmployeeController {

    @Autowired
    EmployeeService employeeService;

    @Autowired
    FileUploadService fileUploadService;

    // todo 获取当前登录员工信息
    @GetMapping("info")
    public Result<EmployeeDTO> getEmployeeInfo() {
        // 获取当前登录的用户id
        Long employeeId = StpKit.ADMIN.getLoginIdAsLong();
        
        EmployeeDTO employeeInfo = employeeService.getEmployeeInfo(employeeId);
        return Result.ok(employeeInfo);
    }

    // todo 上传头像
    @PostMapping("avatar")
    public Result uploadAvatar(@RequestParam("file") MultipartFile file) {

        try{
            if(file.isEmpty()){
                return Result.fail(ResultCodeEnum.FILE_NOT_EXIST);
            }
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            if (!Arrays.asList(".jpg", ".png", ".jpeg", ".gif").contains(extension.toLowerCase())) {
                return Result.fail(ResultCodeEnum.NOT_IS_IMAGE_FILE);
            }
            String upload = fileUploadService.upload(file);
            return Result.ok(upload);
        } catch (IOException e) {
            return Result.fail(ResultCodeEnum.FAIL);
        }
    }

    // todo 获取后台员工列表
    @SaAdminCheckPermission("admin:employee:search")
    @GetMapping("list")
    public Result<PageDTO<EmployeeDTO>> getEmployeeList(PageRequest pageRequest) {
        PageDTO<EmployeeDTO> employeeList = employeeService.getEmployeePage(pageRequest);
        return Result.ok(employeeList);
    }
    // todo 后台添加员工
    @SaAdminCheckPermission("admin:employee:add")
    @PostMapping("")
    @Transactional
    public Result addEmployee(@RequestBody @Valid EmployeeCommand employeeCommand){
        employeeService.addEmployee(employeeCommand);
        return Result.ok();
    }

    // todo 后台修改员工信息
    @SaAdminCheckPermission("admin:employee:update")
    @PutMapping("")
    public Result updateEmployee(@RequestBody EmployeeCommand employeeCommand){
        employeeService.updateEmployee(employeeCommand);
        return Result.ok();
    }


    // todo 后台删除员工信息---删除指定员工(后台账号权限管理-员工管理)
    @DeleteMapping("{employeeId}")
    public Result delete(@PathVariable(value = "employeeId") Long employeeId) {

        // 传Id过去删除
        employeeService.delete(employeeId);

        return Result.ok();
    }

    // todo 后台删除员工-员工关联信息---删除指定员工的角色(后台账号权限管理-员工管理)
    @DeleteMapping("{employeeId}/role/{roleId}")
    public Result deleteRole(@PathVariable("employeeId") Long employeeId, @PathVariable("roleId") Long roleId) {

        employeeService.deleteRole(employeeId, roleId);

        return Result.ok();
    }




}
